package cn.iocoder.yudao.module.resource.controller.admin.userpreference;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.resource.controller.admin.userpreference.vo.UserPreferencePageReqVO;
import cn.iocoder.yudao.module.resource.controller.admin.userpreference.vo.UserPreferenceRespVO;
import cn.iocoder.yudao.module.resource.controller.admin.userpreference.vo.UserPreferenceSaveReqVO;
import cn.iocoder.yudao.module.resource.dal.dataobject.userpreference.UserPreferenceDO;
import cn.iocoder.yudao.module.resource.service.userpreference.UserPreferenceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - 用户偏好设置")
@RestController
@RequestMapping("/lm/user-preference")
@Validated
public class UserPreferenceController {

    @Resource
    private UserPreferenceService userPreferenceService;

    @PostMapping("/create")
    @Operation(summary = "创建用户偏好设置")
    @PreAuthorize("@ss.hasPermission('lm:user-preference:create')")
    public CommonResult<String> createUserPreference(@Valid @RequestBody UserPreferenceSaveReqVO createReqVO) {
        return success(userPreferenceService.createUserPreference(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户偏好设置")
    @PreAuthorize("@ss.hasPermission('lm:user-preference:update')")
    public CommonResult<Boolean> updateUserPreference(@Valid @RequestBody UserPreferenceSaveReqVO updateReqVO) {
        userPreferenceService.updateUserPreference(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户偏好设置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('lm:user-preference:delete')")
    public CommonResult<Boolean> deleteUserPreference(@RequestParam("id") String id) {
        userPreferenceService.deleteUserPreference(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户偏好设置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('lm:user-preference:query')")
    public CommonResult<UserPreferenceRespVO> getUserPreference(@RequestParam("id") String id) {
        UserPreferenceDO userPreference = userPreferenceService.getUserPreference(id);
        return success(BeanUtils.toBean(userPreference, UserPreferenceRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户偏好设置分页")
    @PreAuthorize("@ss.hasPermission('lm:user-preference:query')")
    public CommonResult<PageResult<UserPreferenceRespVO>> getUserPreferencePage(@Valid UserPreferencePageReqVO pageReqVO) {
        PageResult<UserPreferenceDO> pageResult = userPreferenceService.getUserPreferencePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, UserPreferenceRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户偏好设置 Excel")
    @PreAuthorize("@ss.hasPermission('lm:user-preference:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUserPreferenceExcel(@Valid UserPreferencePageReqVO pageReqVO,
                                          HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UserPreferenceDO> list = userPreferenceService.getUserPreferencePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户偏好设置.xls", "数据", UserPreferenceRespVO.class,
                BeanUtils.toBean(list, UserPreferenceRespVO.class));
    }

}